package com.yixing.tech.common.tcp.pc;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

import java.util.concurrent.BlockingDeque;

@Slf4j
public class TcpProducer {

    private final BlockingDeque<TcpMessage> deque;

    public TcpProducer(BlockingDeque<TcpMessage> deque) {
        this.deque = deque;
    }

    public void produce(TcpMessage message) {
        try {
            /** 校验协议消息格式是否正确 */
            if(!message.getMessage().startsWith("AB 66") || !message.getMessage().endsWith("03")) {
                log.debug("message format error:{}", message);
                return;
            }
            if(ObjectUtils.isEmpty(message.getMessage())
                    || message.getMessage().replaceAll(" ", "").length() != 40) {
                log.debug("message format error:{}", message);
                return;
            }
            log.info("produce tcpMessage:{}", message);
            this.deque.putLast(message);
        } catch (InterruptedException e) {
            log.error("tcp生产消息失败: {}", e.getMessage());
        }
    }
}
